GO
/****** Object:  StoredProcedure [dbo].[crosstab]    Script Date: 01/23/2008 00:52:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[crosstab] 
@select varchar(8000),
@sumfunc varchar(100), 
@pvt varchar(100), 
@table varchar(100) 
AS

DECLARE @sql varchar(8000), @delim varchar(1)
SET NOCOUNT ON
SET ANSI_WARNINGS OFF

EXEC ('SELECT ' + @pvt + ' AS pvt INTO ##pvt FROM ' + @table + ' WHERE 1=2')
EXEC ('INSERT INTO ##pvt SELECT DISTINCT ' + @pvt + ' FROM ' + @table + ' WHERE ' 
+ @pvt + ' Is Not Null')

SELECT @sql='',  @sumfunc=stuff(@sumfunc, len(@sumfunc), 1, ' END)' )

SELECT @delim=CASE Sign( CharIndex('char', data_type)+CharIndex('date', data_type) ) 
WHEN 0 THEN '' ELSE '''' END 
FROM tempdb.information_schema.columns 
WHERE table_name='##pvt' AND column_name='pvt'

SELECT @sql=@sql + '''' + convert(varchar(100), pvt) + ''' = ' + 
stuff(@sumfunc,charindex( '(', @sumfunc )+1, 0, ' CASE ' + @pvt + ' WHEN ' 
+ @delim + convert(varchar(100), pvt) + @delim + ' THEN ' ) + ', ' FROM ##pvt

DROP TABLE ##pvt

SELECT @sql=left(@sql, len(@sql)-1)
SELECT @select=stuff(@select, charindex(' FROM ', @select)+1, 0, ', ' + @sql + ' ')

EXEC (@select)
SET ANSI_WARNINGS ON
GO
/****** Object:  Table [dbo].[xref_AppType]    Script Date: 01/23/2008 00:52:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[xref_AppType](
	[appTypeID] [int] IDENTITY(1,1) NOT NULL,
	[appType] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_xref_AppType_1] PRIMARY KEY CLUSTERED 
(
	[appTypeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_xref_AppType_UniqueAppType] UNIQUE NONCLUSTERED 
(
	[appType] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[xref_IDXRef]    Script Date: 01/23/2008 00:52:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[xref_IDXRef](
	[idXRefID] [int] IDENTITY(1,1) NOT NULL,
	[idXRef] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_xref_IDXRef] PRIMARY KEY CLUSTERED 
(
	[idXRefID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[xref_IDXRefData]    Script Date: 01/23/2008 00:52:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[xref_IDXRefData](
	[idXRefID] [int] NOT NULL,
	[appInstanceID] [int] NOT NULL,
	[appID] [nvarchar](255) NOT NULL,
	[commonID] [int] NOT NULL,
 CONSTRAINT [PK_xref_IDXRefData] PRIMARY KEY CLUSTERED 
(
	[idXRefID] ASC,
	[appInstanceID] ASC,
	[commonID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_xref_IDXRefData_commonID] UNIQUE NONCLUSTERED 
(
	[commonID] ASC,
	[idXRefID] ASC,
	[appInstanceID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[xref_AppInstance]    Script Date: 01/23/2008 00:52:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[xref_AppInstance](
	[appInstanceID] [int] IDENTITY(1,1) NOT NULL,
	[appInstance] [nvarchar](50) NOT NULL,
	[appTypeID] [int] NOT NULL,
 CONSTRAINT [PK_xref_AppInstance_1] PRIMARY KEY CLUSTERED 
(
	[appInstanceID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_xref_AppInstance_UniqueAppInstanceName] UNIQUE NONCLUSTERED 
(
	[appInstance] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[xref_IDXRefCommonValues]    Script Date: 01/23/2008 00:52:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[xref_IDXRefCommonValues](
	[commonID] [int] IDENTITY(1,1) NOT NULL,
	[idXRefID] [int] NOT NULL,
	[commonIDDescription] [nvarchar](255) NOT NULL,
 CONSTRAINT [PK_xref_IDXRefCommonValues] PRIMARY KEY CLUSTERED 
(
	[commonID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_xref_IDXRefCommonValues_UniqueCommonIDPerXRefType] UNIQUE NONCLUSTERED 
(
	[idXRefID] ASC,
	[commonIDDescription] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  StoredProcedure [dbo].[GetXRefIdCrossTab]    Script Date: 01/23/2008 00:52:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetXRefIdCrossTab]
	@idXRef nvarchar(255)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    Declare @Sql nvarchar (2000)
	Set @Sql = 'SELECT commonIDDescription as [Common Value] FROM ApplicationXRefIDs Where idXRef = ''' + @idXRef + ''' Group By commonIDDescription'
	Execute crosstab @Sql, 'max([appId])', 'appInstance', 'ApplicationXRefIDs'

END
GO
/****** Object:  View [dbo].[ApplicationXRefIDs]    Script Date: 01/23/2008 00:52:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[ApplicationXRefIDs]
AS
SELECT     dbo.xref_IDXRefData.appID, dbo.xref_AppInstance.appInstance, dbo.xref_IDXRef.idXRef, dbo.xref_IDXRefCommonValues.commonIDDescription
FROM         dbo.xref_IDXRefData FULL OUTER JOIN
                      dbo.xref_IDXRef INNER JOIN
                      dbo.xref_IDXRefCommonValues ON dbo.xref_IDXRef.idXRefID = dbo.xref_IDXRefCommonValues.idXRefID ON 
                      dbo.xref_IDXRefData.idXRefID = dbo.xref_IDXRef.idXRefID AND 
                      dbo.xref_IDXRefData.commonID = dbo.xref_IDXRefCommonValues.commonID FULL OUTER JOIN
                      dbo.xref_AppInstance ON dbo.xref_IDXRefData.appInstanceID = dbo.xref_AppInstance.appInstanceID
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[31] 4[17] 2[14] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1[50] 4[25] 3) )"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "xref_IDXRefCommonValues"
            Begin Extent = 
               Top = 127
               Left = 412
               Bottom = 220
               Right = 595
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "xref_IDXRef"
            Begin Extent = 
               Top = 70
               Left = 616
               Bottom = 148
               Right = 767
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "xref_AppInstance"
            Begin Extent = 
               Top = 24
               Left = 0
               Bottom = 117
               Right = 151
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "xref_IDXRefData"
            Begin Extent = 
               Top = 6
               Left = 227
               Bottom = 114
               Right = 378
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 135' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'ApplicationXRefIDs'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N'0
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'ApplicationXRefIDs'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=2 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'ApplicationXRefIDs'
GO
/****** Object:  StoredProcedure [dbo].[UpdateApplicationXRefId]    Script Date: 01/23/2008 00:52:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[UpdateApplicationXRefId]
	@ApplicationName nvarchar(50),
	@IdXRef nvarchar(50),
	@CommonIdDescription nvarchar(255),
	@ApplicationId nvarchar(255)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    Declare @ApplicationInstanceId int
	Declare @CommonId int
	Declare @IdXRefId int
	
	Select @ApplicationInstanceId = appInstanceID From xref_AppInstance Where appInstance = @ApplicationName
	Select @IdXRefId = idXRefID From xref_IDXRef Where idXRef = @IdXRef
	Select @CommonId = commonID From xref_IDXRefCommonValues Where commonIDDescription = @CommonIdDescription And idXrefID = @IdXRefId

	If(Exists(Select 1 From xref_IDXRefData Where appInstanceID = @ApplicationInstanceId And commonID = @CommonId And idXRefID = @IdXRefId))
	Begin
		Update xref_IDXRefData
		Set
			appID = @ApplicationId
		Where
			appInstanceID = @ApplicationInstanceId 
			And 
			commonID = @CommonId 
			And 
			idXRefID = @IdXRefId
	End
	Else
	Begin
		Insert Into xref_IDXRefData
			(idxRefID, appInstanceID, appID, commonID)
		Values
			(@IdXRefId, @ApplicationInstanceId, @ApplicationId, @CommonId)
	End
END
GO
/****** Object:  View [dbo].[AppInstanceDetailView]    Script Date: 01/23/2008 00:52:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[AppInstanceDetailView]
AS
SELECT     dbo.xref_AppType.appType, dbo.xref_AppInstance.appInstance
FROM         dbo.xref_AppType INNER JOIN
                      dbo.xref_AppInstance ON dbo.xref_AppType.appTypeID = dbo.xref_AppInstance.appTypeID
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "xref_AppType"
            Begin Extent = 
               Top = 6
               Left = 38
               Bottom = 84
               Right = 189
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "xref_AppInstance"
            Begin Extent = 
               Top = 6
               Left = 227
               Bottom = 99
               Right = 378
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'AppInstanceDetailView'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=1 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'AppInstanceDetailView'
GO
/****** Object:  ForeignKey [FK_xref_AppInstance_xref_AppType]    Script Date: 01/23/2008 00:52:20 ******/
ALTER TABLE [dbo].[xref_AppInstance]  WITH CHECK ADD  CONSTRAINT [FK_xref_AppInstance_xref_AppType] FOREIGN KEY([appTypeID])
REFERENCES [dbo].[xref_AppType] ([appTypeID])
GO
ALTER TABLE [dbo].[xref_AppInstance] CHECK CONSTRAINT [FK_xref_AppInstance_xref_AppType]
GO
/****** Object:  ForeignKey [FK_xref_IDXRefCommonValues_xref_IDXRef]    Script Date: 01/23/2008 00:52:24 ******/
ALTER TABLE [dbo].[xref_IDXRefCommonValues]  WITH CHECK ADD  CONSTRAINT [FK_xref_IDXRefCommonValues_xref_IDXRef] FOREIGN KEY([idXRefID])
REFERENCES [dbo].[xref_IDXRef] ([idXRefID])
GO
ALTER TABLE [dbo].[xref_IDXRefCommonValues] CHECK CONSTRAINT [FK_xref_IDXRefCommonValues_xref_IDXRef]
GO
/****** Object:  ForeignKey [FK_xref_IDXRefData_xref_AppInstance]    Script Date: 01/23/2008 00:52:27 ******/
ALTER TABLE [dbo].[xref_IDXRefData]  WITH CHECK ADD  CONSTRAINT [FK_xref_IDXRefData_xref_AppInstance] FOREIGN KEY([appInstanceID])
REFERENCES [dbo].[xref_AppInstance] ([appInstanceID])
GO
ALTER TABLE [dbo].[xref_IDXRefData] CHECK CONSTRAINT [FK_xref_IDXRefData_xref_AppInstance]
GO
/****** Object:  ForeignKey [FK_xref_IDXRefData_xref_IDXRef]    Script Date: 01/23/2008 00:52:27 ******/
ALTER TABLE [dbo].[xref_IDXRefData]  WITH CHECK ADD  CONSTRAINT [FK_xref_IDXRefData_xref_IDXRef] FOREIGN KEY([idXRefID])
REFERENCES [dbo].[xref_IDXRef] ([idXRefID])
GO
ALTER TABLE [dbo].[xref_IDXRefData] CHECK CONSTRAINT [FK_xref_IDXRefData_xref_IDXRef]
GO
/****** Object:  ForeignKey [FK_xref_IDXRefData_xref_IDXRefCommonValues]    Script Date: 01/23/2008 00:52:27 ******/
ALTER TABLE [dbo].[xref_IDXRefData]  WITH CHECK ADD  CONSTRAINT [FK_xref_IDXRefData_xref_IDXRefCommonValues] FOREIGN KEY([commonID])
REFERENCES [dbo].[xref_IDXRefCommonValues] ([commonID])
GO
ALTER TABLE [dbo].[xref_IDXRefData] CHECK CONSTRAINT [FK_xref_IDXRefData_xref_IDXRefCommonValues]
GO
